package com.hnepri.fang.ftx2.fetch.index;

import com.hnepri.fang.ftx2.fetch.APageBridge;
import com.hnepri.fang.ftx2.fetch.IPage;
import com.hnepri.fang.ftx2.model.Residential;
import com.hnepri.util.DBUtil;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取小区首页的评级信息
 */
public class ResidentialIndexFetch {
    private String city;
    private String storePath;

    public ResidentialIndexFetch(String city, String storePath) {
        this.city = city;
        this.storePath = storePath;
    }

    private List<Residential> getData() {
        List<Residential> ls = new ArrayList<>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        //这个sql是查找没有明细的小区简介数据
        String sql = "SELECT t1.id,t1.domain " +
                "from xiaoqu t1 where t1.city='" + city + "' " +
                "and not EXISTS(SELECT 1 from xiaoqupj t2 where t1.id=t2.fid)";

        try {
            con = DBUtil.getConnection();
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            Residential obj = null;
            while (rs.next()) {
                obj = new Residential();

                String id = rs.getString(1);
                String url = rs.getString(2);
                obj.setId(id);
                obj.setDomain(url);

                ls.add(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                DBUtil.closeResource(con, ps, rs);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return ls;
    }
    public void go(int threadLen) {
        String path = storePath + "/" + city + "/index";
        File file = new File(path);
        File pdir = file.getParentFile();
        if (!pdir.exists()) {
            pdir.mkdir();
        }
        if (!file.exists()) {
            file.mkdir();
        }
        List<Residential> ls = this.getData();

        int len = ls.size();
        int groupStep = len / threadLen;//每个线程查询多少页面
        int extralPage = len % threadLen;

        int startIndex = 0;
        APageBridge pageBridge = new ResidentialIndexPageBridgeConcrete();
        IPage page = new ResidentialIndexPageImpl();
        pageBridge.setPage(page);

        for(int i = 0; i < threadLen; i++) {
            ResidentialIndexThread residentialIndexThread = new ResidentialIndexThread(pageBridge, ls,  path, startIndex, startIndex + groupStep - 1);
            residentialIndexThread.start();
            startIndex += groupStep;
        }
        if (extralPage > 0) {
            ResidentialIndexThread residentialIndexThread = new ResidentialIndexThread(pageBridge, ls, path, startIndex, len -1);
            residentialIndexThread.start();
        }
    }
    public static void main(String[] args) {
        String city = "xx";
        String storePath = "c:/小区";
        ResidentialIndexFetch residentialIndexFetch = new ResidentialIndexFetch(city, storePath);

        residentialIndexFetch.go(4);
    }
}
